using System;
using System.Xml;

namespace stockinfo
{

  /// <summary>
  /// Summary description for Class1
  /// </summary>
  public class Stock
  {
    string companyName;
    string stockSymbol;
    string exchange;
    float lastTrade;
    float change;
    float pctChange;
    float dayLow;
    float dayHigh;
    string volume;
    DateTime lastUpdateDate;


    public Stock()
    {
    }


    public Stock(string symbol)
    {
      stockSymbol = symbol;
    }


    public string CompanyName
    {
      get { return companyName; }
      set { companyName = value; }
    }


    public string StockSymbol
    {
      get { return stockSymbol; }
      set { stockSymbol = value; }
    }


    public string Exchange
    {
      get { return exchange; }
      set { exchange = value; }
    }


    public float LastTrade
    {
      get { return lastTrade; }
      set { lastTrade = value; }
    }


    public float Change
    {
      get { return change; }
      set { change = value; }
    }


    public float PctChange
    {
      get { return pctChange; }
      set { pctChange = value; }
    }


    public float DayLow
    {
      get { return dayLow; }
      set { dayLow = value; }
    }


    public float DayHigh
    {
      get { return dayHigh; }
      set { dayHigh = value; }
    }


    public string Volume
    {
      get { return volume; }
      set { volume = value; }
    }


    public DateTime LastUpdateDate
    {
      get { return lastUpdateDate; }
      set { lastUpdateDate = value; }
    }


    public void UpdateFromMarket()
    {
      string stockUrl = "http://wu.apple.com/fq/applewidgets/quote.asp?key=tHisIsApplewidgeTs&symbols=" + stockSymbol;
      XmlTextReader stockXml = null;
      stockXml = new XmlTextReader(stockUrl);
      if(stockXml != null)
      {
        XmlDocument stockDoc = new XmlDocument();
        stockDoc.Load(stockXml);

        XmlNode node = stockDoc.DocumentElement;
        if(node.Name == "INDEXSET")
        {
          node = node.FirstChild;
          if (node.Name == "QUOTE")
          {
            XmlNode childNode = node.FirstChild;
            while (childNode != null)
            {
              if (childNode.Name == "COMPANYNAME")
              {
                companyName = childNode.InnerText;
              }
              else if (childNode.Name == "EXCHANGE")
              {
                exchange = childNode.InnerText;
              }
              else if (childNode.Name == "SYMBOL")
              {
                stockSymbol = childNode.InnerText;
              }
              else if (childNode.Name == "LASTTRADE")
              {
                lastTrade = float.Parse(childNode.InnerText);
              }
              else if (childNode.Name == "CHANGE")
              {
                change = float.Parse(childNode.InnerText);
              }
              else if (childNode.Name == "PCTCHANGE")
              {
                pctChange = float.Parse(childNode.InnerText);
              }
              else if (childNode.Name == "DAYLOW")
              {
                dayLow = float.Parse(childNode.InnerText);
              }
              else if (childNode.Name == "DAYHIGH")
              {
                dayHigh = float.Parse(childNode.InnerText);
              }
              else if (childNode.Name == "VOLUME")
              {
                volume = childNode.InnerText;
              }

              childNode = childNode.NextSibling;
            }

            lastUpdateDate = DateTime.Now;
          }
        }
        
        if (stockXml != null)
        {
          stockXml.Close();
        }
      }
    }


    public string ToString()
    {
      return companyName + " [" + stockSymbol + "]";
    }
  }

}